Declare AbstractSystem as Enzyme inactive_type#4553
Merged
ChrisRackauckas merged 1 commit intoMay 23, 2026
Conversation
The existing `inactive_noinl` rule on `getproperty(::AbstractSystem, ::Symbol)` is not enough to keep Enzyme's runtime-activity dispatch from trying to track a `System` as a differentiable value. Without `inactive_type`, `Enzyme.gradient(set_runtime_activity(Reverse), Const(loss), p)` over a closure that transitively captures an MTK-generated problem trips a `MethodError: no method matching MixedDuplicated(::System, ::System)` in `create_activity_wrapper`. The `System` type holds symbolic metadata only — never numeric derivative data — so declaring the type inactive is semantically correct and unblocks one more layer of the Enzyme-through-MTK-`remake` stack documented in SciML/SciMLSensitivity.jl#1359. Co-Authored-By: Chris Rackauckas <accounts@chrisrackauckas.com>
|
@oscardssmith this seems sus to me |
Member
|
Why would this be sus? Symbolic spaces are discrete and not differentiable. |
|
can you guarantee that all types which subclass AbstractSystem cannot contain a vector{float64} anywhere (including via a closed over function)? |
Member
|
Even better, by design any float in there has to be treated as a constant by definition. |
ChrisRackauckas
added a commit
to SciML/SciMLSensitivity.jl
that referenced
this pull request
May 23, 2026
…tivity The three `@test_broken Enzyme.gradient` blocks (`mtk.jl` line 168, `parameter_initialization.jl` "Adjoint through Prob (Enzyme)", `desauty_dae_mwe.jl` "Enzyme through init") previously called `Enzyme.gradient(Enzyme.Reverse, loss, tunables)` — i.e. relied on `Enzyme.gradient` to infer annotations on a closure that captures a mutable `ODEProblem`/`NonlinearProblem`. That triggers `EnzymeMutabilityException` on a captured-mutable, which per Billy Moses (EnzymeAD/Enzyme.jl#3117 close) is correct Enzyme behavior, not a bug: `Enzyme.gradient` can't annotate captures, so the user must do it. Apply the documented user-side pattern in all three blocks: Enzyme.gradient( Enzyme.set_runtime_activity(Enzyme.Reverse), Enzyme.Const(loss), tunables, ) Plus, for `desauty_dae_mwe.jl`, pin `solve(iprob2, NewtonRaphson())` so Enzyme's type analysis does not trip on the polyalgorithm Union the default NonlinearSolve dispatch would otherwise emit. (`mtk.jl` already uses `Rodas5P()`; `parameter_initialization.jl` uses an `ODEProblem` solve with `GaussAdjoint` sensealg, no NonlinearSolve polyalg in the AD hot path.) These tests **stay `@test_broken`** — the activity layer is correct after this change, but the chain still hits a `MixedDuplicated` / `Core.SimpleVector` MethodError further down in Enzyme's runtime-activity wrapping for MTK-`System` / `NonlinearSolution` types. Tracked in #1359, with one upstream piece already filed at SciML/ModelingToolkit.jl#4553 (declare `AbstractSystem` as `inactive_type`). When the remaining upstream lifts, flipping `@test_broken` → `@test` is the only change needed in these blocks. Refs #1323, #1359; EnzymeAD/Enzyme.jl#3117. Co-Authored-By: Chris Rackauckas <accounts@chrisrackauckas.com>
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Please ignore until reviewed by @ChrisRackauckas.
Summary
AbstractSystemis symbolic metadata — never carries numerical derivative data. The existinginactive_noinlrule ongetproperty(::AbstractSystem, ::Symbol)keeps Enzyme from trying to differentiatesys.xproperty accesses, but does not keep Enzyme's runtime-activity dispatch from trying to track theSystemitself.When a user follows the documented Enzyme idioms —
Const(loss)for a closure capturing aConst-correct mutable problem, plusset_runtime_activity(Reverse)for activity inference — and the closure transitively carries aSystem(which is the case for every MTK-generatedNonlinearFunction/ODEFunctionvia theObservedFunctionCache{System,...}field), Enzyme's runtime-activity wrapping trips:MixedDuplicatedonly accepts aRefValue-boxed shadow, so Enzyme's machinery is asking for a shadow it can't produce. The right answer is to tell Enzyme to never wrapSystemin the first place — that's whatinactive_type(::Type{<:AbstractSystem}) = truedoes.Effect
This is necessary but not sufficient to flip the three
@test_broken Enzyme.gradientblocks in SciML/SciMLSensitivity.jl'stest/mtk.jl,test/parameter_initialization.jl,test/desauty_dae_mwe.jl. With this PR applied locally on Julia 1.12.6 (Enzyme 0.13.148),Enzyme.gradient(set_runtime_activity(Reverse), Const(loss), tunables)advances past theMixedDuplicated(::System, ::System)error and onto the next upstream layer (TypeError: in new, expected DataType, got Type{Core.SimpleVector}). The remaining chain is tracked in SciML/SciMLSensitivity.jl#1359.Refs
supports_initialization→get_jumps)Test plan
Enzyme.gradient(set_runtime_activity(Reverse), Const(loss), tunables)on parameter_initialization's "Adjoint through Prob (Enzyme)" closure advances past theMixedDuplicated(::System, ::System)error after this declaration is added